﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using FoodBank.Core.IoC;
using FoodBank.Core.Models;
using FoodBank.Core.Repository;
using FoodBank.Data;
using WebMatrix.WebData;

namespace FoodBank.Web
{
    public class ViewBagInjectionFilter : ActionFilterAttribute
    {
        

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(WebSecurity.IsAuthenticated)
            {
                var repository = WindsorContainerFactory.Container(null).Resolve<IRepository<User>>();
                var user = repository.RetrieveMany(e => e.UserName.ToUpperInvariant() == WebSecurity.CurrentUserName.ToUpperInvariant()).First();

                var viewBag = filterContext.Controller.ViewBag;
                viewBag.CurrentUserFullName = string.Format(CultureInfo.InvariantCulture, "{0} {1}", user.FirstName, user.LastName);
                viewBag.CurrentUserEmail = user.Email;
            }

            base.OnActionExecuting(filterContext);
        }
    }
}